package com.xuecheng.content.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xuecheng.content.mapper.CourseTeacherMapper;
import com.xuecheng.content.model.po.CourseTeacher;
import com.xuecheng.content.service.CourseTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Queue;

/**
 * @Author Fan
 * @Date 2024-01-15 13:53
 * @ClassName: CourseTeacherServiceImpl
 * @Description: 教资管理接口
 */

@Service
public class CourseTeacherServiceImpl implements CourseTeacherService {

    @Autowired
    CourseTeacherMapper courseTeacherMapper;

    /**
     * @author 79464
     * @description 查询教资接口
     * @date 2024-01-15 13:59
     */
    @Override
    public List<CourseTeacher> queryCourseTeacher(Long id) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,id);
        List<CourseTeacher> courseTeacherList = courseTeacherMapper.selectList(queryWrapper);
        return courseTeacherList;
    }

    /**
     * @param : 实体类参数
     * @author 79464
     * @description 新增或修改教资
     * @date 2024-01-15 14:10
     */

    @Override
    public CourseTeacher addOrUpdateCourseTeacher(CourseTeacher courseTeacher) {
        if (courseTeacher.getId() == null){
            courseTeacher.setCreateDate(LocalDateTime.now());
            int id = courseTeacherMapper.insertTeacher(courseTeacher);
            return courseTeacherMapper.selectById(id);
        }else {
            LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseTeacher::getId,courseTeacher.getId());
            courseTeacherMapper.update(courseTeacher,queryWrapper);
            return courseTeacherMapper.selectById(courseTeacher.getId());
        }
    }

    @Override
    public void detTeacher(long courseId, long teacherId) {
        LambdaQueryWrapper<CourseTeacher> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CourseTeacher::getCourseId,courseId).eq(CourseTeacher::getId,teacherId);
        courseTeacherMapper.delete(queryWrapper);
    }

}
